using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LockerRack.Exceptions;
using LockerRack.Helpers;

namespace LockerRack.Drawers
{
    internal static class DrawerExtension
    {
        public static void SaveFiles(this Drawer drawer, string folder, List<Exception> exceptions)
        {
            foreach (var file in drawer.Files)
            {
                LockerException lex;
                file.SaveContent(folder, out lex);
                if (lex != null)
                {
                    exceptions.Add(lex);
                }
            }
        }

        public static void SaveDrawers(this Drawer drawer, string folder, List<Exception> exceptions)
        {
            if (drawer == null)
                return;

            foreach (var d in drawer.Drawers)
            {
                LockerException lex;
                d.Save(folder, out lex);
                if (lex != null)
                {
                    exceptions.Add(lex);
                }
            }
        }

        public static void LoadFiles(this Drawer drawer, List<Exception> exceptions)
        {
            var folder = drawer.FilesFolder;
            if(!PathUtility.CheckDirectory(folder))
                return;

            var drawerFile = Path.Combine(drawer.Folder, Constants.DRAWER_FILE_NAME);
            var files = Directory.EnumerateFiles(folder).Where(f => !f.Equals(drawerFile, StringComparison.OrdinalIgnoreCase));
            try
            {
                foreach (var file in files)
                {
                    LockerException ex;
                    var f = new DrawerFile(file, drawer.TypesContainer);
                    f.LoadContent(file, out ex);
                    if(ex != null)
                    {
                        exceptions.Add(ex);
                        continue;
                    }
                    drawer.Files.Add(f);
                }
            }
            catch (Exception ex)
            {
                exceptions.Add(new LockerException("Can't load files", ex));
            }
        }

        public static void LoadDrawersTree(this Drawer drawer, List<Exception> exceptions)
        {
            var folder = drawer.Folder;
            if (!PathUtility.CheckDirectory(folder))
                return;

            var dirs = Directory.EnumerateDirectories(folder);
            foreach (var dir in dirs)
            {
                var initParams = new Drawer.InitParams { Folder = dir, FilesFolder = drawer.FilesFolder, TypesContainer = drawer.TypesContainer, Parent = drawer };
                var d = Drawer.OpenDrawerTree(initParams);
                if(initParams.Exception != null)
                {
                    exceptions.Add(initParams.Exception);
                    continue;
                }

                drawer.Drawers.Add(d);
            }
        }

        public static void LoadDrawersPlain(this Drawer drawer, List<Exception> exceptions)
        {
            var folder = drawer.Folder;
            if (!PathUtility.CheckDirectory(folder))
                return;

            var dirs = Directory.EnumerateDirectories(folder);
            foreach (var dir in dirs)
            {
                var initParams = new Drawer.InitParams { Folder = dir, FilesFolder = drawer.FilesFolder, TypesContainer = drawer.TypesContainer, Parent = drawer };
                var d = Drawer.OpenDrawerPlain(initParams);
                if (initParams.Exception != null)
                {
                    exceptions.Add(initParams.Exception);
                    continue;
                }

                drawer.Drawers.Add(d);
            }
        }
    }
}